<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Configurable
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Configurable.php 7 2009-11-14 22:42:08Z nkeyes $
 */

/**
 * 	Interface for Configurable classes
 *
 * @category   Bushido
 * @package    Bushido_Configurable
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 */
class Bushido_Configurable extends Bushido_Object
{
	protected $allowedConfigOptions = array();
	protected $configOptions = array();
	
	
	/**
	 * 
	 * @param $customFormat
	 * @param $keyQuote
	 * @param $valueQuote
	 * @param $keyValueDelimeter
	 * @param $pairDelimeter
	 * @return array
	 */
	public function getConfigOptions($customFormat = null, $keyQuote = null, $valueQuote=null, $keyValueDelimeter = null, $pairDelimeter = null)
	{
		if(isset($customFormat) && $customFormat)
		{
			$returnString = "";
			foreach($this->configOptions as $key => $value)
			{
				$returnString .= $keyQuote . $key . $keyQuote . $keyValueDelimeter . $valueQuote . $value . $valueQuote . $pairDelimeter;
			}
			return $returnString;
		}
		else
		{
			return $this->configOptions;
		}
	}
	
	/**
	 * 
	 * @param $key
	 * @return string
	 */
	public function getConfigOption($key)
	{
		if(false !== array_search($key, $this->getAllowedConfigOptions()) && array_key_exists($key, $this->getConfigOptions()))
		{
			return $this->configOptions[$key];
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 
	 * @return array
	 */
	public function getAllowedConfigOptions()
	{
		return $this->allowedConfigOptions;
	}
	
	/**
	 * 
	 * @param $key
	 * @param $value
	 * @return boolean
	 */
	public function setConfigOption($key, $value)
	{
		if(false !== array_search($key, $this->getAllowedConfigOptions()))
		{
			$this->configOptions[$key] = $value;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 
	 * @param $options
	 * @return void
	 */
	public function setConfigOptions($options)
	{
		if (is_array($options))
		{
			foreach($options as $key => $value)
			{
				$this->setConfigOption($key, $value);
			}
		}
	}
	
	
	/**
	 * 
	 * @param $testArray
	 * @return array
	 */
	public function filterAllowedConfigOptions($testArray)
	{
		return array_intersect_key($testArray, $this->getAllowedConfigOptions);
	}
	
	
	/**
	 * 
	 * @param $options
	 * @return void
	 */
	public function setAllowedConfigOptions($options)
	{
		$this->allowedConfigOptions = $options;
	}
	
	
	/**
	 * 
	 * @param $options
	 * @return void
	 */
	public function addAllowedConfigOptions($options)
	{
		$this->allowedConfigOptions = array_merge($this->allowedConfigOptions, $options);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Bushido_Object#setAccess()
	 */
	public function setAccess(Zend_Acl $acl)
	{
		$acl->allow(Bushido_App::session()->appconfig->defaultRole, $this->getResourceId());
	}
	
}
